﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EasyCore.Excel
{

    /// <summary>
    /// 列配置项：暂只支持最多两行标题
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ColumnOption<T>
    {
        /// <summary>
        /// Excel字段名称
        /// </summary>
        public string ColumnName { get; set; }

        /// <summary>
        /// 列类型
        /// </summary>
        public ColumnType ColumnType { get; set; } = ColumnType.MainData;

        /// <summary>
        /// 数据库字段名称或属性名称
        /// </summary>
        public string FieldName { get; set; }

        /// <summary>
        /// 列宽
        /// </summary>
        public int ColumnWidth { get; set; } = 12;

        /// <summary>
        /// 序号：用于排序
        /// </summary>
        public int Order { get; set; } = 0;

        /// <summary>
        /// 是否隐藏列
        /// </summary>
        public bool IsHide { get; set; } = false;

        /// <summary>
        /// 格式化值: 序号、对象值，返回值
        /// </summary>
        public Func<int, T, string> FormatColumnVal { get; set; }

        /// <summary>
        /// 孩子节点
        /// </summary>
        public List<ColumnOption<T>> Children { get; set; }

        /// <summary>
        /// 添加子标题
        /// </summary>
        /// <param name="columnAction"></param>
        public void AddChildrenColumn(Action<ColumnOption<T>> columnAction)
        {
            if (columnAction == null) return;
            var option = new ColumnOption<T>();
            if (Children == null) Children = new List<ColumnOption<T>>();
            if (columnAction != null) columnAction(option);
            Children.Add(option);
        }

        /// <summary>
        /// 是否存在子标题
        /// </summary>
        /// <returns></returns>
        public bool IsExistChildren()
        {
            if (Children == null || !Children.Any()) return false;
            return true;
        }

        /// <summary>
        ///获得列的跨度
        /// </summary>
        /// <returns></returns>
        public int GetColumnSpan()
        {
            if (Children == null || !Children.Any()) return 1;
            return Children.Count;
        }
    }

    public enum ColumnType
    {
        MainIndex, //主序号列     
        MainGroupKey, //标识分组的列
        MainData, //主数据的列
        SubIndex,//子序号列
        SubData,  //子数据的列
    }
}
